package com.gzx.plugin.tjzy.modular.supplier.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhs.trans.service.impl.DictionaryTransService;
import com.gzx.plugin.tjzy.modular.supplier.bo.GzxSupplierAddOrEditBo;
import com.gzx.plugin.tjzy.modular.supplier.bo.GzxSupplierQueryBo;
import com.gzx.plugin.tjzy.modular.supplier.entity.GzxSupplier;
import com.gzx.plugin.tjzy.modular.supplier.mapper.GzxSupplierMapper;
import com.gzx.plugin.tjzy.modular.supplier.service.GzxSupplierListService;
import com.gzx.plugin.tjzy.modular.supplier.service.GzxSupplierService;
import com.gzx.plugin.tjzy.modular.supplier.vo.GzxSupplierVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.consts.GzxDictConstant;
import vip.xiaonuo.common.tenant.TenantContext;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @author zjt
 * @description
 * @date 2025/3/6 18:23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxSupplierServiceImpl implements GzxSupplierService, InitializingBean {

    private final GzxSupplierMapper supplierMapper;

    private final DictionaryTransService dictionaryTransService;

    private final GzxSupplierListService supplierListService;

    /**
     * 分页查询供应商
     *
     * @param supplierQueryBo
     * @return
     */
    @DS("sms")
    @Override
    public Page<GzxSupplierVo> pageList(GzxSupplierQueryBo supplierQueryBo) {
        return supplierMapper.selectVoPage(new Page<>(supplierQueryBo.getCurrent(), supplierQueryBo.getSize()), Wrappers.<GzxSupplier>lambdaQuery().orderByDesc(GzxSupplier::getId));
    }

    /**
     * 供应商新增或者修改
     *
     * @param supplierAddOrEditBo
     */
    @DS("sms")
    @Override
    public void addOrEdit(GzxSupplierAddOrEditBo supplierAddOrEditBo) {
        GzxSupplier supplier = MapstructUtils.convert(supplierAddOrEditBo, GzxSupplier.class);
        if (supplierAddOrEditBo.getId() == null) {
            supplierMapper.insert(supplier);
        } else {
            supplierMapper.updateById(supplier);
        }
    }

    /**
     * 获取所有供应商
     *
     * @return
     */
    @DS("sms")
    @Override
    public List<GzxSupplierVo> list() {
        return supplierMapper.selectVoList(null);
    }

    /**
     * 删除供应商
     *
     * @param id
     */
    @DS("sms")
    @Override
    public void del(Long id) {
        supplierMapper.deleteById(id);
    }

    /**
     * 根据供应商编码获取Bean
     *
     * @param supplierCode
     * @return
     */
    @DS("sms")
    @Override
    public GzxSupplier getSupplierByCode(String supplierCode) {
        return supplierMapper.selectOne(Wrappers.<GzxSupplier>lambdaQuery()
                .eq(GzxSupplier::getSupplierCode, supplierCode));
    }

    @DS("sms")
    @Override
    public void afterPropertiesSet() {
        refreshTransCache();
    }

    private void refreshTransCache() {
        CompletableFuture.supplyAsync(() -> {
            // 使用redis能解决共享问题，但是性能没有直接取缓存的好。
            dictionaryTransService.makeUseRedis();
            try {
                TenantContext.disableTenantFilter();
                List<GzxSupplier> supplierList = supplierListService.listAll();
                dictionaryTransService.refreshCache(GzxDictConstant.SUPPLIER, supplierList.stream().collect(Collectors.toMap(GzxSupplier::getSupplierCode, GzxSupplier::getSupplierName)));
            } finally {
                TenantContext.enableTenantFilter();
                TenantContext.clear();
            }
            return null;
        }).exceptionally(ex -> {
            log.error("刷新供应商缓存发生异常:", ex);
            return null;
        });
    }
}
